package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import selenium.WebDriverWrapper;

import java.util.concurrent.TimeUnit;

public class TicketsPage extends BasePage{
    private static final  String ADULTS_TICKET_LOCATOR = "//div[@class='adults_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final  String INFANTS_TICKET_LOCATOR = "//div[@class='infants_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final  String CHILDREN_TICKET_LOCATOR = "//div[@class='children_block passengers_wrapper']/div[@class='persons']/div[%d]";
    private static final By SEARCH_BUTTON = By.id("start_search");
    private static final By POP_UP_WARNING = By.xpath("//*[@class=\"popup error_popup\"]");

    public TicketsPage(WebDriverWrapper driver){
        super (driver);
        URL = "http://rozetka.com.ua/travel/IEV/MOW/";
        driver.get(URL);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    public void buyTickets(int adults, int infant, int children){
        driver.findElement(By.xpath(String.format(ADULTS_TICKET_LOCATOR, adults))).click();
        driver.findElement(By.xpath(String.format(INFANTS_TICKET_LOCATOR,infant))).click();
        driver.findElement(By.xpath(String.format(CHILDREN_TICKET_LOCATOR,children))).click();
        driver.findElement(SEARCH_BUTTON).click();
    }

    public boolean isError(){
        return driver.findElements(POP_UP_WARNING).size()>0;
    }
}
